﻿#region Imported Namespaces

using System;
using System.Globalization;
using System.Text;

#endregion

namespace Christian.Helle.PocketNews.PocketPC.Diagnostics
{
    /// <summary>
    /// Helper class for building Error Messages
    /// </summary>
    public static class ErrorMessageBuilder
    {
        /// <summary>
        /// Builds an error message
        /// </summary>
        /// <param name="exception">Exception containg the details of the error</param>
        /// <returns>Returns a string describing the error</returns>
        public static string BuildExceptionMessage(Exception exception)
        {
            var body = new StringBuilder();
            body.AppendFormat(CultureInfo.InvariantCulture, "DATE:\n{0}", DateTime.Now);
            body.Append("\n\n");
#if !PocketPC
            body.AppendFormat(CultureInfo.InvariantCulture, "MACHINE:\n{0}", Environment.MachineName);
            body.Append("\n\n");
#endif
            body.AppendFormat(CultureInfo.InvariantCulture, "OPERATING SYSTEM:\n{0}", Environment.OSVersion);
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "EXCEPTION TYPE:\nException");
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "MESSAGE:\n{0}", exception.Message);
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "STACK TRACE:\n{0}", exception.StackTrace);

            if (exception.InnerException != null)
            {
                body.Append("\n\n");
                body.AppendFormat(CultureInfo.InvariantCulture, "INNER EXCEPTION\n");
                body.Append(BuildExceptionMessage(exception.InnerException));
            }

            return body.ToString();
        }

        /// <summary>
        /// Builds an error message
        /// </summary>
        /// <param name="exception">Exception containg the details of the error</param>
        /// <param name="message">Error description</param>
        /// <returns>Returns a string describing the error</returns>
        public static string BuildExceptionMessage(Exception exception, string message)
        {
            var body = new StringBuilder();
            body.AppendFormat(CultureInfo.InvariantCulture, "ERROR MESSAGE:\n{0}", message);
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "DATE:\n{0}", DateTime.Now);
            body.Append("\n\n");
#if !PocketPC
            body.AppendFormat(CultureInfo.InvariantCulture, "MACHINE:\n{0}", Environment.MachineName);
            body.Append("\n\n");
#endif
            body.AppendFormat(CultureInfo.InvariantCulture, "OPERATING SYSTEM:\n{0}", Environment.OSVersion);
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "EXCEPTION TYPE:\nException");
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "MESSAGE:\n{0}", exception.Message);
            body.Append("\n\n");
            body.AppendFormat(CultureInfo.InvariantCulture, "STACK TRACE:\n{0}", exception.StackTrace);

            if (exception.InnerException != null)
            {
                body.Append("\n\n");
                body.AppendFormat(CultureInfo.InvariantCulture, "INNER EXCEPTION\n");
                body.Append(BuildExceptionMessage(exception.InnerException));
            }

            return body.ToString();
        }
    }
}